public without sharing class ContractProductHandler {
    

    //在合同仪器更新的时候 如果全部安装完成则 终端业务机会完结
    public static void finishOpportunity(List<ContractProduct__c> cpList,Map<Id,ContractProduct__c> oldMap ){
    	Set<Id> oppSet = new Set<Id>();

    	for (ContractProduct__c cp : cpList) {
    		ContractProduct__c old  = oldMap.get(cp.Id );
    		if( cp.Quantity__c != null && cp.Quantity__c != 0 && cp.InstallQuantity__c == cp.Quantity__c ){
    			if(cp.Opportunity__c != null )oppSet.add(cp.Opportunity__c );
    		}
    	}

    	for(ContractProduct__c cp : [select Id,Contract__c,Opportunity__c,InstallQuantity__c,Quantity__c from ContractProduct__c 
    		where Opportunity__c in: oppSet  and Product__r.Category0__c = '仪器' ]){
    		if(cp.Quantity__c != cp.InstallQuantity__c ){
                oppSet.remove(cp.Opportunity__c);
            }

    	}

    	if(oppSet.isEmpty()  ) return;

    	// Set<Id> oppSet = new Set<Id>();
    	// for (Contract__c  con  ) {
    		
    	// 	if(con.Opportunity__c != null ) oppSet.add(con.Opportunity__c);
    	// }
    	List<Opportunity> oppList = new List<Opportunity>();
    	for(Opportunity opp : [select Id from Opportunity  where Id in:oppSet and RecordType.DeveloperName = 'CustomerOpportunity' ] ){
           
    		opp.StageName = '合同签订';
    		oppList.add(opp);
    	}
    	if(!oppList.isEmpty())update oppList;

    }
    public static void upgradeStage(Set<Id> conSet){
        Set<Id> oppSet =new Set<Id>();
        for(ContractProduct__c cp: [select Id,Opportunity__c from ContractProduct__c  where Contract__c in:conSet and   Opportunity__c != null  ]){
            oppSet.add(cp.Opportunity__c);
        }
        for(ContractReagentInformation__c cp: [select Id,Opportunity__c from ContractReagentInformation__c  where Contract__c in:conSet and   Opportunity__c != null  ]){
            oppSet.add(cp.Opportunity__c);
        }

        for(ContractChangeList__c cp: [select Id,ContractProduct__r.Opportunity__c  from ContractChangeList__c  where Contract__c in:conSet and   ContractProduct__r.Opportunity__c  != null  ]){
            oppSet.add(cp.ContractProduct__r.Opportunity__c );
        }
        
        Map<Id,String> oppMap = new Map<Id,String>();
        for(ContractProduct__c cp: [select Id,Opportunity__c,Opportunity__r.RecordType.DeveloperName, Opportunity__r.StageName , Contract__r.Status__c from ContractProduct__c  where Opportunity__c in:oppSet   ]){

            if(cp.Opportunity__r.StageName == '合作确认'|| cp.Opportunity__r.StageName == '合同签订' )oppSet.remove(cp.Opportunity__c);
            if(cp.Contract__r.Status__c != '已生效')oppSet.remove(cp.Opportunity__c);

            oppMap.put(cp.Opportunity__c, cp.Opportunity__r.RecordType.DeveloperName);

        }
        for(ContractReagentInformation__c cp: [select Id,Opportunity__c,Opportunity__r.RecordType.DeveloperName, Opportunity__r.StageName , Contract__r.Status__c from ContractReagentInformation__c  where Opportunity__c in:oppSet and Opportunity__r.RecordType.DeveloperName = 'DealerOpportunity'  ]){
            
            if(cp.Opportunity__r.StageName == '合作确认'|| cp.Opportunity__r.StageName == '合同签订' )oppSet.remove(cp.Opportunity__c);
            if(cp.Contract__r.Status__c != '已生效')oppSet.remove(cp.Opportunity__c);

            oppMap.put(cp.Opportunity__c, cp.Opportunity__r.RecordType.DeveloperName);

        }
        List<Opportunity> oppList = new List<Opportunity>();
        for(Id oppId : oppSet){
            Opportunity opp = new Opportunity();
            opp.Id = oppId ;
            opp.StageName = oppMap.get(oppId) == 'OverseaDearlerOpportunity' ?'合作确认'  :'合同签订'; 
            oppList.add(opp);
        }
        if(!oppList.isEmpty())update oppList;
    }
    public static void updateNum(List<ContractProduct__c> cpList){
       
        Map<Id,Boolean> accMap = new Map<Id,Boolean>();

        for(ContractProduct__c cp : cpList){
            if(cp.CoCustomer__c != null ){
                    accMap.put(cp.CoCustomer__c , false);
            }
        }
        if(accMap.isEmpty())return;

        for (Account acc  : [select Id ,DeliveryControl__c from Account where Id in: accMap.keySet() ]) {
            if(acc.DeliveryControl__c == '按季管控'){
                accMap.put(acc.Id , true);
            }else{
                accMap.remove(acc.Id );
            }
        }
        if(accMap.isEmpty())return;

        for(ContractProduct__c cp : cpList){
            if(cp.CoCustomer__c != null && accMap.containsKey(cp.CoCustomer__c) ){
                if(cp.Quantity__c == null ) cp.Quantity__c = cp.OriginNumber__c;
                cp.ApplicationQuantity__c = cp.Quantity__c;

            }
        }


    }
}